// 扁平化转树 parentId为null为根节点
// [{id:1, parentId:null,name:'广东}, {id:2, parentId:2, name:'深圳'}, {id:3, parentId:2, name:'广州}....]
let arr = [{id:1, parentId:null,name:'广东'}, {id:2, parentId:1, name:'深圳'}, {id:3, parentId:2, name:'广州'}]

let obj = {}, result = []
for(let item of arr) {
    obj[item.id] = item
    item.children = []
    if(item.parentId == null) {
        result.push(item)
    }
}
let treeList = []
arr.forEach(item => {
    if(obj[item.parentId]) {
        obj[item.parentId].children.push(item)
    }
    else {
        treeList.push(item)
    }
})
console.log(result);